﻿using Stote.Forms;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Stote
{
    public partial class FrmMain : BaseForm
    {
        Timer timer = new Timer();

        #region 构造函数
        public FrmMain()
        {
            InitializeComponent();
            tsslCurrentUser.Text = $"当前用户：{FrmLogin.CurrentUser?.Account} {FrmLogin.CurrentUserType.UserTypeName}";
            tsslCurrentTime.Text = $"当前时间：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}";

            timer.Interval = 1000;
            timer.Tick += Timer_Tick;
            timer.Start();

            splitContainer1.IsSplitterFixed = true;
            splitContainer1.SplitterMoved += SplitContainer1_SplitterMoved;
            pbToggle.Click += PbToggle_Click;
            PbToggle_Click(null, null);

            lblSplit.Visible = false;
            lblCurrentPageName.Visible = false;

            btnUserType.Click += BtnUserType_Click;
            btnUser.Click += BtnUser_Click;
            btnStore.Click += BtnStore_Click;
            btnStoreArea.Click += BtnStoreArea_Click;
            btnProduct.Click += BtnProduct_Click;
            btnPutIn.Click += BtnPutIn_Click;
            btnOutbound.Click += BtnOutbound_Click;
            btnInventory.Click += BtnInventory_Click;
            btnTemperature.Click += BtnTemperature_Click;
            btnLogout.Click += BtnLogout_Click;
            btnSystemExit.Click += BtnSystemExit_Click;
        }


        #endregion

        #region 定时器
        private void Timer_Tick(object sender, EventArgs e)
        {
            tsslCurrentTime.Text = $"当前时间：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}";
        }
        #endregion

        #region 打开窗体
        private void BtnUserType_Click(object sender, EventArgs e)
        {
            ToggleSelected("用户类型");
            OpenForm(new FrmUserType(), "用户类型");
        }

        private void BtnUser_Click(object sender, EventArgs e)
        {
            ToggleSelected("用户管理");
            OpenForm(new FrmUser(), "用户管理");
        }

        private void BtnStore_Click(object sender, EventArgs e)
        {
            ToggleSelected("仓库管理");
            OpenForm(new FrmStore(), "仓库管理");
        }

        private void BtnStoreArea_Click(object sender, EventArgs e)
        {
            ToggleSelected("分区管理");
            OpenForm(new FrmArea(), "分区管理");
        }

        private void BtnProduct_Click(object sender, EventArgs e)
        {
            ToggleSelected("产品管理");
            OpenForm(new FrmProduct(), "产品管理");
        }

        private void BtnPutIn_Click(object sender, EventArgs e)
        {
            ToggleSelected("入库管理");
            OpenForm(new FrmStorageRecord(), "入库管理");
        }

        private void BtnOutbound_Click(object sender, EventArgs e)
        {
            ToggleSelected("出库管理");
            OpenForm(new FrmOutboundRecord(), "出库管理");
        }

        private void BtnInventory_Click(object sender, EventArgs e)
        {
            ToggleSelected("库存盘点");
            OpenForm(new FrmInventory(), "库存盘点");
        }

        private void BtnTemperature_Click(object sender, EventArgs e)
        {
            ToggleSelected("温控管理");
            OpenForm(new FrmTemperature(), "温控管理");
        }

        private void BtnLogout_Click(object sender, EventArgs e)
        {
            Application.Restart();
            Application.Exit();
        }

        private void BtnSystemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public void ToggleSelected(string title)
        {
            foreach (Control control in splitContainer1.Panel1.Controls)
            {
                if (control is Button)
                {
                    if (control.Text.Trim() == title || toolTip1.GetToolTip(control) == title)
                    {
                        control.BackColor = Color.FromArgb(1, 88, 219);
                    }
                    else
                    {
                        control.BackColor = Color.FromArgb(1, 62, 153);
                    }
                }
            }
        }

        public void OpenForm(Form form, string title)
        {
            form.Dock = DockStyle.Fill;
            form.TopLevel = false; // 没有顶层显示
            form.FormBorderStyle = FormBorderStyle.None;
            form.BackColor = Color.White;
            form.Padding = new Padding(10, 0, 10, 0);
            form.Show();

            panelContainer.Controls.Clear();
            panelContainer.Controls.Add(form);

            lblCurrentPageName.Text = title;
            lblCurrentPageName.Visible = true;
            lblSplit.Visible = true;
        }
        #endregion

        #region 菜单展开和折叠
        private void PbToggle_Click(object sender, EventArgs e)
        {
            if (splitContainer1.SplitterDistance > 60)
            {
                splitContainer1.SplitterDistance = 60;
                Collapse();
            }
            else
            {
                splitContainer1.SplitterDistance = 230;
                Expand();
            }
        }

        private void SplitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {
            if (splitContainer1.SplitterDistance <= 230)
            {
                Collapse();
            }
            else
            {
                Expand();
            }
        }

        private void Expand()
        {
            pbToggle.Image = Properties.Resources.toggle_left;
            btnUserType.Text = "    用户类型";
            btnUser.Text = "    用户管理";
            btnStore.Text = "    仓库管理";
            btnStoreArea.Text = "    分区管理";
            btnProduct.Text = "    产品管理";
            btnPutIn.Text = "    入库管理";
            btnOutbound.Text = "    出库管理";
            btnInventory.Text = "    库存盘点";
            btnTemperature.Text = "    温控管理";
            btnLogout.Text = "    系统锁屏";
            toolTip1.SetToolTip(btnUserType, "");
            toolTip1.SetToolTip(btnUser, "");
            toolTip1.SetToolTip(btnStore, "");
            toolTip1.SetToolTip(btnStoreArea, "");
            toolTip1.SetToolTip(btnProduct, "");
            toolTip1.SetToolTip(btnPutIn, "");
            toolTip1.SetToolTip(btnOutbound, "");
            toolTip1.SetToolTip(btnInventory, "");
            toolTip1.SetToolTip(btnTemperature, "");
            toolTip1.SetToolTip(btnLogout, "");
        }

        private void Collapse()
        {
            pbToggle.Image = Properties.Resources.toggle_right;
            btnUserType.Text = "";
            btnUser.Text = "";
            btnStore.Text = "";
            btnStoreArea.Text = "";
            btnProduct.Text = "";
            btnPutIn.Text = "";
            btnOutbound.Text = "";
            btnInventory.Text = "";
            btnTemperature.Text = "";
            btnLogout.Text = "";
            toolTip1.SetToolTip(btnUserType, "用户类型");
            toolTip1.SetToolTip(btnUser, "用户管理");
            toolTip1.SetToolTip(btnStore, "仓库管理");
            toolTip1.SetToolTip(btnStoreArea, "分区管理");
            toolTip1.SetToolTip(btnProduct, "产品管理");
            toolTip1.SetToolTip(btnPutIn, "入库管理");
            toolTip1.SetToolTip(btnOutbound, "出库管理");
            toolTip1.SetToolTip(btnInventory, "库存盘点");
            toolTip1.SetToolTip(btnTemperature, "温控管理");
            toolTip1.SetToolTip(btnLogout, "系统锁屏");
        }

        #endregion

        
    }
}
